<template>
	<div class="home-container">
		<el-row :gutter="15" class="home-card-one mb15">
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" class="home-media home-media-lg">
				<div class="home-card-item flex">
					<div class="flex-margin flex w100" :class="` home-one-animation1`">
						<div class="flex-auto">
							<span class="font30">{{ countInfo.onLineInstance }}个</span>
							<span class="ml5 font16" style="color: #6690F9;">{{ countInfo.totalInstance }}个</span>
							<div class="mt10">在线实例个数</div>
						</div>
						<div class="home-card-item-icon flex"
							:style="{ background: `var(${'--next-color-success-lighter'})` }">
							<i class="flex-margin font32" :style="{ color: `'--el-color-success'` }">
								<el-icon><ele-Star /></el-icon>
							</i>
						</div>
					</div>
				</div>
			</el-col>
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" class="home-media home-media-lg">
				<div class="home-card-item flex">
					<div class="flex-margin flex w100" :class="` home-one-animation1`">
						<div class="flex-auto">
							<span class="font30">{{ countInfo.fileCount }}</span>
							<div class="mt10">文件总数</div>
						</div>
						<div class="home-card-item-icon flex"
							:style="{ background: `var(${'--next-color-success-lighter'})` }">
							<i class="flex-margin font32" :style="{ color: `'--el-color-success'` }">
								<el-icon><ele-Document /></el-icon>
							</i>
						</div>
					</div>
				</div>
			</el-col>
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" class="home-media home-media-lg">
				<div class="home-card-item flex">
					<div class="flex-margin flex w100" :class="` home-one-animation1`">
						<div class="flex-auto">
							<span class="font30">{{ countInfo.uploadTps }}</span>
							<div class="mt10">上传接口(TPS)</div>
						</div>
						<div class="home-card-item-icon flex"
							:style="{ background: `var(${'--next-color-success-lighter'})` }">
							<i class="flex-margin font32 fa fa-meetup" :style="{ color: `'--el-color-success'` }">
								<el-icon><ele-Upload /></el-icon>
							</i>
						</div>
					</div>
				</div>
			</el-col>
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" class="home-media home-media-lg">
				<div class="home-card-item flex">
					<div class="flex-margin flex w100" :class="` home-one-animation1`">
						<div class="flex-auto">
							<span class="font30">{{ countInfo.downloadTps }}</span>
							<div class="mt10">下载接口(TPS)</div>
						</div>
						<div class="home-card-item-icon flex"
							:style="{ background: `var(${'--next-color-success-lighter'})` }">
							<i class="flex-margin font32 fa fa-meetup" :style="{ color: `'--el-color-success'` }">
								<el-icon><ele-Download /></el-icon>
							</i>
						</div>
					</div>
				</div>
			</el-col>
		</el-row>
		<el-row class="home-card-two mb15">
			<el-col>
				<div class="home-card-item">
					<div style="height: 100%" ref="homeLineRef"></div>
				</div>
			</el-col>
		</el-row>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, defineComponent, onMounted, onUnmounted, ref, watch, nextTick, onActivated } from 'vue';
import * as echarts from 'echarts';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import pinia from '/@/stores/index';
import { useTagViewInfoStore } from '/@/stores/tag-view-info';
import useStatisticsApi from '/@/api/jdfsystem/statistics';
import { StatisticsCountModel, StatisticsFileModel } from '/@/interfaces/JdfsystemModel';
import MESSAGE from '/@/enums/message-enum';
import { stat } from 'fs';

interface State {
	countInfo: StatisticsCountModel,
	fileCountInfo: StatisticsFileModel,
	myCharts: any[],
	charts: any,
}

let global: any = {
	homeChartOne: null,
	homeChartTwo: null,
	homeCharThree: null,
	dispose: [null, '', undefined],
};

export default defineComponent({
	name: 'home',
	setup() {
		const homeLineRef = ref();
		const tagViewStore = useTagViewInfoStore(pinia);
		const storesThemeConfig = useThemeConfig();
		const { themeConfig } = storeToRefs(storesThemeConfig);
		const isTagsViewCurrenFull = tagViewStore.getIsTagsViewCurrenFull();
		const statistaicsApi = useStatisticsApi();
		const state = reactive<State>({
			countInfo: {
				onLineInstance: 0,
				totalInstance: 0,
				fileCount: 0,
				uploadTps: 0,
				downloadTps: 0,
			},
			fileCountInfo: {
				dayList: [],
				fileCountList: []
			},
			myCharts: [],
			charts: {
				theme: '',
				bgColor: '',
				color: '#303133',
			},
		});
		const initCount = async () => {
			if (!global.dispose.some((b: any) => b === global.homeChartOne)) global.homeChartOne.dispose();
			global.homeChartOne = <any>echarts.init(homeLineRef.value, state.charts.theme);
			const response = await statistaicsApi.count();
			const { code, data } = response.data;
			if (code === MESSAGE.OK) {
				state.countInfo = { ...data };
			}
		};
		const loadFileCount = async () => {
			const response = await statistaicsApi.fileCount(30);
			const { code, data } = response.data;
			if (code === MESSAGE.OK) {
				state.fileCountInfo = { ...data };
			}
		};
		// 折线图
		const initLineChart = async () => {
			if (!global.dispose.some((b: any) => b === global.homeChartOne)) {
				global.homeChartOne.dispose();
			}
			global.homeChartOne = <any>echarts.init(homeLineRef.value, state.charts.theme);
			await loadFileCount();
			const option = {
				backgroundColor: state.charts.bgColor,
				title: {
					text: '近30天文件数',
					x: 'left',
					textStyle: { fontSize: '15', color: state.charts.color },
				},
				grid: { top: 70, right: 20, bottom: 30, left: 30 },
				tooltip: { trigger: 'axis' },
				legend: { data: ['文件数'], right: 0 },
				xAxis: {
					data: state.fileCountInfo.dayList,
				},
				yAxis: [
					{
						type: 'value',
						name: '数量',
						splitLine: { show: true, lineStyle: { type: 'dashed', color: '#f5f5f5' } },
					},
				],
				series: [
					{
						name: '文件数',
						type: 'line',
						symbolSize: 6,
						symbol: 'circle',
						smooth: true,
						data: state.fileCountInfo.fileCountList,
						lineStyle: { color: '#9E87FF' },
						itemStyle: { color: '#9E87FF', borderColor: '#9E87FF' },
						areaStyle: {
							color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
								{ offset: 0, color: '#9E87FFb3' },
								{ offset: 1, color: '#9E87FF03' },
							]),
						},
						emphasis: {
							itemStyle: {
								color: {
									type: 'radial',
									x: 0.5,
									y: 0.5,
									r: 0.5,
									colorStops: [
										{ offset: 0, color: '#9E87FF' },
										{ offset: 0.4, color: '#9E87FF' },
										{ offset: 0.5, color: '#fff' },
										{ offset: 0.7, color: '#fff' },
										{ offset: 0.8, color: '#fff' },
										{ offset: 1, color: '#fff' },
									],
								},
								borderColor: '#9E87FF',
								borderWidth: 2,
							},
						},
					},
				],
			};
			(<any>global.homeChartOne).setOption(option);
		};
		// 批量设置 echarts resize
		const initEchartsResizeFun = () => {
			nextTick(() => {
				global.homeChartOne.resize();
			});
		};
		// 销毁chart
		const destroyChart = ()=>{
			global.homeChartOne.dispose();
		};
		// 定时刷新
		const startInterval = () => {
			setInterval(() => {
				initCount();
				// 刷新折线图
				initLineChart();
				initEchartsResizeFun();
			}, 5000);
		};
		// 批量设置 echarts resize
		const initEchartsResize = () => {
			window.addEventListener('resize', initEchartsResizeFun);
		};
		// 页面加载时
		onMounted(() => {
			initEchartsResize();
			startInterval();
		});
		onUnmounted(()=>{
			destroyChart();
		});
		// 由于页面缓存原因，keep-alive
		onActivated(() => {
			initEchartsResizeFun();
		});
		// 监听 vuex 中的 tagsview 开启全屏变化，重新 resize 图表，防止不出现/大小不变等
		watch(
			() => isTagsViewCurrenFull,
			() => {
				initEchartsResizeFun();
			}
		);
		// 监听 vuex 中是否开启深色主题
		watch(
			() => themeConfig.value.isIsDark,
			(isIsDark) => {
				nextTick(() => {
					state.charts.theme = isIsDark ? 'dark' : '';
					state.charts.bgColor = isIsDark ? 'transparent' : '';
					state.charts.color = isIsDark ? '#dadada' : '#303133';
					initLineChart();
					initCount();
				});
			},
			{
				deep: true,
				immediate: true,
			}
		);
		return {
			homeLineRef,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
$homeNavLengh: 8;

.home-container {
	overflow: hidden;

	.home-card-one,
	.home-card-two,
	.home-card-three {
		.home-card-item {
			width: 100%;
			height: 130px;
			border-radius: 4px;
			transition: all ease 0.3s;
			padding: 20px;
			overflow: hidden;
			background: var(--el-color-white);
			color: var(--el-text-color-primary);
			border: 1px solid var(--next-border-color-light);

			&:hover {
				box-shadow: 0 2px 12px var(--next-color-dark-hover);
				transition: all ease 0.3s;
			}

			&-icon {
				width: 70px;
				height: 70px;
				border-radius: 100%;
				flex-shrink: 1;

				i {
					color: var(--el-text-color-placeholder);
				}
			}

			&-title {
				font-size: 15px;
				font-weight: bold;
				height: 30px;
			}
		}
	}

	.home-card-one {
		@for $i from 0 through 3 {
			.home-one-animation#{$i} {
				opacity: 0;
				animation-name: error-num;
				animation-duration: 0.5s;
				animation-fill-mode: forwards;
				animation-delay: calc($i/10) + s;
			}
		}
	}

	.home-card-two,
	.home-card-three {
		.home-card-item {
			height: 400px;
			width: 100%;
			overflow: hidden;

			.home-monitor {
				height: 100%;

				.flex-warp-item {
					width: 25%;
					height: 111px;
					display: flex;

					.flex-warp-item-box {
						margin: auto;
						text-align: center;
						color: var(--el-text-color-primary);
						display: flex;
						border-radius: 5px;
						background: var(--next-bg-color);
						cursor: pointer;
						transition: all 0.3s ease;

						&:hover {
							background: var(--el-color-primary-light-9);
							transition: all 0.3s ease;
						}
					}

					@for $i from 0 through $homeNavLengh {
						.home-animation#{$i} {
							opacity: 0;
							animation-name: error-num;
							animation-duration: 0.5s;
							animation-fill-mode: forwards;
							animation-delay: calc($i/10) + s;
						}
					}
				}
			}
		}
	}
}
</style>
